package com.gurun.camera.addDevice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gurun.camera.BaseActivity;
import com.gurun.camera.R;
import com.gurun.camera.R2;
import com.gurun.camera.addDevice.presenter.AddDevicePresenterImpl;
import com.gurun.camera.addDevice.presenter.IAddDevicePresenter;
import com.gurun.camera.addDevice.view.AddDeviceView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddCameraDeviceActivity extends BaseActivity implements AddDeviceView {

    @BindView(R2.id.edt_dev_alias)
    EditText edtDevAlias;
    @BindView(R2.id.edt_dev_id)
    EditText edtDevId;
    @BindView(R2.id.btn_check_add_device)
    Button btnAdd;

    private IAddDevicePresenter IAddDevicePresenter;

    public static void startActivity(Context from) {
        Intent intent = new Intent(from, AddCameraDeviceActivity.class);
        from.startActivity(intent);
    }

    public static void startActivity(Context from, String devId) {
        Intent it = new Intent(from, AddCameraDeviceActivity.class);
        it.putExtra("device_id", devId);
        from.startActivity(it);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_camera_device);
        unbinder = ButterKnife.bind(this);

        String device_id = getIntent().getStringExtra("device_id");
        if (device_id != null) {
            edtDevId.setText(device_id);
        }

        IAddDevicePresenter = new AddDevicePresenterImpl(this);
    }

    @OnClick(R2.id.btn_check_add_device)
    void onClickAddDev() {
        IAddDevicePresenter.checkDevStatus(edtDevId.getText().toString());
    }

    @Override
    public void onAddDevSuccess() {

        Toast.makeText(getApplicationContext(), "add success", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onAddDevFailed(String errorMsg) {

        Toast.makeText(getApplicationContext(), "Add Failed:" + " " + errorMsg, Toast.LENGTH_LONG).show();
    }


    @Override
    public void onDeviceAddedByOther(String deviceId, String account) {

    }

    @Override
    public void onDeviceAddedByMyself() {

    }

    @Override
    public void onDeviceOffline(String devId) {

    }

    @Override
    public void onDeviceCanAdd(String deviceId) {

        IAddDevicePresenter.addDevice(edtDevId.getText().toString(), edtDevAlias.getText().toString());
    }

    @Override
    public void onDeviceIdIllegal(String result) {
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDeviceStatusCheckFailed(String msg) {
        Toast.makeText(getApplicationContext(), "add failed:" + " " + msg, Toast.LENGTH_LONG).show();
    }
}
